package com.seveneight.baijiesteward.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.fastjson.JSON;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.bean.UserBean;
import com.seveneight.baijiesteward.http.JsonFormat;
import com.seveneight.baijiesteward.utils.MDialog;
import com.seveneight.baijiesteward.utils.MMessageDialog;
import com.seveneight.baijiesteward.utils.PreferencesUtil;
import com.seveneight.baijiesteward.utils.Setting;
import com.seveneight.baijiesteward.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by aaa on 2018/1/14.
 */

public abstract class BaseFragment extends BaseMainFragment {
    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;
    public Context mContext;
    public View view;
    public Setting setting;
    private MDialog mDialog;
    private MMessageDialog dialog;
    public JsonFormat jsonFormat;
    public UserBean userBean;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getContentView(), container, false);
        return view;
    }

    public <T extends View> T findView(int id) {
        return view.findViewById(id);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initUser();
        initData();
        setListener();
    }
    //处理回调
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent){
        if (messageEvent.getType()==110){
            userBean= JSON.parseObject(PreferencesUtil.get(getContext(),PreferencesUtil.userinfo,""),UserBean.class);
        }else if (messageEvent.getType()==111){
            userBean=null;
        }
        BusCall(messageEvent);
    }
    public void BusCall(MessageEvent messageEvent){

    }

    public void initUser() {
        setting = Setting.getInstance(mContext);
        jsonFormat = JsonFormat.getInstance(mContext);
    }

    protected abstract int getContentView();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void setListener();

    /**
     * 处理回退事件
     *
     * @return
     */
    @Override
    public boolean onBackPressedSupport() {
        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
            _mActivity.finish();
            getApp().exit();
        } else {
            TOUCH_TIME = System.currentTimeMillis();
            ToastUtil.show(_mActivity, getResources().getString(R.string.press_again_exit));
        }
        return true;
    }

    public MyApplication getApp() {
        return MyApplication.getInstance();
    }

    public <T> ObservableTransformer<T, T> setThread() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    public void showDialog(String title) {
        dialog = new MMessageDialog(mContext, title);
        dialog.showDialog();
    }

    public void showLoading(String title) {
        mDialog = new MDialog(mContext, title);
        mDialog.showLoadingDialog();
    }

    public void hideLoading() {
        if (mDialog != null) {
            mDialog.hideDialog();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
